﻿using PlantsZombies.Common.Plant;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;

namespace PlantsZombies.Common.Bullet
{
    [PlantZombie(SpeciesType.FaShi)]
    public class FaShiBullet : BulletObject
    {
        public FaShiBullet(ImageObject[] images, GameObject gameObject)
             : base(new Size(100, 100), images, gameObject)
        {

        }
        protected override unsafe void CheckAttack(ListGameObject* ptr)
        {
            for (int i = _bullets.Count - 1; i >= 0; i--)
            {
                var item = _bullets[i];
                if (item.Type == 14)
                {
                    var bulletRect = new Rectangle(item.X, item.Y, BulletSize.Width, BulletSize.Height);
                    for (int j = _gameObject.Column + 1; j < _gameObject.UseMaxColumn; j++)
                    {
                        var p = ptr + _gameObject.Row * _gameObject.MaxColumn + j;
                        if (p->Game.Count == 0) continue;

                        for (int k = 0; k < p->Game.Count; k++)
                        {
                            var game = p->Game[k];
                            if (game == null) continue;
                            if (!game.IsZombie()) continue;
                            var rect = new Rectangle(game.Location.X, game.Location.Y, game.Size.Width, game.Size.Height);
                            if (bulletRect.IntersectsWith(rect))
                            {
                                //_bullets.Remove(item);
                                if ((game.Life -= _gameObject.Damage) <= 0)
                                    p->Game.RemoveAt(k);
                            }
                        }
                    }
                }
                else if (item.Type == 4 || item.Type == 5)
                {
                    if (item.Index > item.OriginaIndex) continue;
                    var p = ptr + item.Row * _gameObject.MaxColumn + item.Col;
                    if (*p == null || p->Game.Count == 0) continue;
                    var games = p->Game;
                    for (int j = 0; j < p->Game.Count; j++)
                    {
                        var game = p->Game[j];
                        if (game == null) continue;
                        if (!game.IsZombie()) continue;
                        if ((game.Life -= _gameObject.Damage) <= 0)
                            p->Game.RemoveAt(j);
                    }
                }
            }
        }
        public override unsafe void Move(ListGameObject* ptr)
        {
            int maxWidth = _gameObject.StartX + _gameObject.CellWidth * _gameObject.UseMaxColumn;
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                if (item.Type == 14)
                {
                    item.X += 15;
                    if (item.X + BulletSize.Width > maxWidth)
                        _bullets.RemoveAt(i);

                    if (item.Index >= item.EndIndex)
                    {
                        item.Index = item.OriginaIndex;
                    }
                }
                else if(item.Type == 4 || item.Type == 5)
                {
                    if (item.Index > item.EndIndex)
                        _bullets.RemoveAt(i);
                }
            }
            var fashi = _gameObject as GameFaShi;
            if (fashi._bodyType == 3 && _gameObject.CurPlayIndex == 14)
            {
                _bullets.Add(new BulletPoint
                {
                    Index = 27,
                    EndIndex = 32,
                    X = _gameObject.Location.X + (_gameObject.CellHeight - _gameObject.Size.Width) / 2 + (int)(20 * _gameObject.ZoomX),
                    Y = _gameObject.Location.Y + (_gameObject.CellHeight - BulletSize.Height) / 2 - (int)(24 * _gameObject.ZoomX),
                    Type = 14,
                });
            }
            else if (fashi._bodyType == 4 && _gameObject.CurPlayIndex == 19)
            {
                for (int i = 0; i < _gameObject.MaxRow; i++)
                {
                    for (int j = 0; j < _gameObject.UseMaxColumn; j++)
                    {
                        var p = ptr + i * _gameObject.MaxColumn + j;
                        if (p->Game.Count == 0) continue;
                        for (int k = 0; k < p->Game.Count; k++)
                        {
                            var game = p->Game[k];
                            if (!game.IsZombie() || game.Column > _gameObject.UseMaxColumn) continue;

                            _bullets.Add(new BulletPoint
                            {
                                Index = 21,
                                EndIndex = 26,
                                Type = 4,
                                Id = game.Id,
                                X = game.Location.X - (int)(_gameObject.ZoomX * 40),
                                Y = game.Location.Y - (int)(_gameObject.ZoomY * 8),
                                Row = game.Row,
                                Col = game.Column
                            });
                        }
                    }
                }
            }
            else if (fashi._bodyType == 5 && _gameObject.CurPlayIndex == 22)
            {
                int col = _gameObject.Column+1;
                for (int i = 0; i < _gameObject.MaxRow; i++)
                {
                    for (int j = col; j < _gameObject.UseMaxColumn; j++)
                    {
                        _bullets.Add(new BulletPoint
                        {
                            Index = 0,
                            EndIndex = 19,
                            Id = _gameObject.Id,
                            X = _gameObject.StartX + j * _gameObject.CellWidth - (int)(_gameObject.ZoomX * 0),
                            Y = _gameObject.StartY + i * _gameObject.CellHeight - (int)(_gameObject.ZoomY * 0),
                            Row = i,
                            Col = j,
                            Type=5
                        });
                    }
                }
            }
        }
        public override void Play(IntPtr hdc)
        {
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                if (item.Index > item.EndIndex) continue;
                if (item.Index >= _imagesBullet.Length) continue;
                var img = _imagesBullet[item.Index];
                GDI32.DrawImageTransparenc(hdc, img.BitmapPtr, item.X, item.Y, img.Width, img.Height, BulletSize.Width, BulletSize.Height);
                item.Index++;
            }
           
        }
    }
}
